<?php

use Biz\service\Store\StoreBasicService;

/**
 * 搜索门店-发布器，添加门店
 * Class ListAction
 */
class SearchAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws Exception
     */
    public function main()
    {
        $pageSize = 20;
        $keyword = $this->getRequest()->getQuery('keyword', '');
        $page = $this->getRequest()->getQuery('page', 0);
        $location = $this->getRequest()->getQuery('location', '');
        $page = $page < 1 ? 1 : abs($page);
        $longitude = '';
        $latitude = '';
        $locationArr = !empty($location) ? json_decode($location, true) : [];
        if ($locationArr) {
            $longitude = $locationArr['lng'];
            $latitude = $locationArr['lat'];
        }

        list($list, $total) = StoreBasicService::searchStoreByKeyword($keyword, $page, $pageSize, $longitude, $latitude);
        $data = [
            'list' => $list,
            'is_over' => ($page * $pageSize) >= $total ? 1 : 0
        ];
        return $this->setSuccess($data);
    }

}